package core

import (
	"openzbox.com/driver/host"
	"openzbox.com/driver/host/nuvoton/nuc98x"
	"openzbox.com/log"
	"openzbox.com/misc/grtm"
)

type Bus struct {
	Driver string `json:"driver"`
	Dev    string `json:"dev"`
	Addr   string `json:"addr"`
}

type Mbox struct {
	Name         string `json:"name"`
	ID           string `json:"id"`
	Manager      string `json:"manager"`
	Mobile       string `json:"mobile"`
	Maintenancer string `json:"maintenancer"`
	Email        string `json:"email"`

	Ports    map[string]Port `json:"-"` //[]Port    `json:"ports"`
	Commands map[string]Poll `json:"commands"`

	SysCtrl  []IOCtrl              `json:"sys_ctrl"`
	Bus      []Bus                 `json:"bus"`
	Services []Service             `json:"services"`
	gm       *grtm.GrManager       `json:"-"`
	PortCfgs map[string]PortConfig `json:"ports"` //[]Port    `json:"ports"`

}

var Mybox *Mbox

/*初始化配置*/
func InitBox(mbox *Mbox) error {

	if _, err := host.Init(); err != nil {
		return err
	}

	err := mbox.LoadConfig()
	if err != nil {
		log.Errorf("parse mbox.json err.%s", err)
	}
	//log.Infof("mbox config:%v", mbox)

	mbox.gm = grtm.NewGrManager()
	//初始云服务
	mbox.LoadIoTServices()
	//初始基本goio
	mbox.InitIOCtrl()
	//初始端口
	mbox.InitPorts()

	return nil
}

func (mbox *Mbox) SyncRtcTimeFromInternet() error {
	err := nuc98x.SyncRtcTimeFromInternet()
	if err != nil {
		return err
	}
	return nil
}
